import 'package:flutter/material.dart';
import './chat/message_page.dart';
import './contacts/contacts.dart';
import './found/found.dart';
import './personal/personal.dart';
import './my_cards_page.dart'; // 导入新的页面
enum ItemType {
  GroupChat,
  AddFriends,
  QrCode,
  Payments,
  Help,
  MyCards // 添加新的枚举项
}
class App extends StatefulWidget {
  @override
  MainState createState() => MainState();
}
class MainState extends State<App> {
  var _currentIndex = 0;
  MessagePage? message;
  Contacts? contacts;
  Personal? me;
  Found? found;
  Widget? currentPage() {
    switch (_currentIndex) {
      case 0:
        message ??= MessagePage();
        return message;
      case 1:
        contacts ??= Contacts();
        return contacts;
      case 2:
        found ??= Found();
        return found;
      case 3:
        me ??= Personal();
        return me;
      default:
        return null;
    }
  }
  PopupMenuItem<ItemType> _popupMenuItem(String title, {String? imagePath, IconData? icon, ItemType? type}) {
    return PopupMenuItem(
      value: type, // 设置菜单项的值
      child: Row(
        children: <Widget>[
          imagePath != null
              ? Image.asset(
            imagePath,
            width: 32.0,
            height: 32.0,
          )
              : SizedBox(
            width: 32.0,
            height: 32.0,
            child: Icon(
              icon,
              color: Colors.white,
            ),
          ),
          Container(
            padding: const EdgeInsets.only(left: 20.0),
            child: Text(
              title,
              style: TextStyle(color: Colors.white),
            ),
          ),
        ],
      ),
    );
  }
  void _onMenuItemSelected(ItemType value) {
    switch (value) {
      case ItemType.GroupChat:
        print('发起群聊');
        break;
      case ItemType.AddFriends:
        print('添加朋友');
        break;
      case ItemType.QrCode:
        print('扫一扫');
        break;
      case ItemType.Payments:
        print('收付款');
        break;
      case ItemType.Help:
        print('帮助与反馈');
        break;
      case ItemType.MyCards:
        print('我的卡片');
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => MyCardsPage()),
        );
        break;
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('微信'),
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              Navigator.pushNamed(context, 'search');
            },
            child: Icon(
              Icons.search,
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 30.0, right: 20.0),
            child: GestureDetector(
              onTap: () {
                showMenu(
                  context: context,
                  position: RelativeRect.fromLTRB(500.0, 76.0, 10.0, 0.0),
                  items: <PopupMenuEntry<ItemType>>[
                    _popupMenuItem('发起群聊', imagePath: 'images/icon_menu_group.png', type: ItemType.GroupChat),
                    _popupMenuItem('添加朋友', imagePath: 'images/icon_menu_addfriend.png', type: ItemType.AddFriends),
                    _popupMenuItem('扫一扫', imagePath: 'images/icon_menu_scan.png', type: ItemType.QrCode),
                    _popupMenuItem('收付款', icon: Icons.crop_free, type: ItemType.Payments),
                    _popupMenuItem('帮助与反馈', icon: Icons.email, type: ItemType.Help),
                    _popupMenuItem('我的卡片', icon: Icons.card_membership, type: ItemType.MyCards), // 添加新的菜单项
                  ],
                ).then((value) {
                  if (value != null) {
                    _onMenuItemSelected(value);
                  }
                });
              },
              child: Icon(Icons.add),
            ),
          ),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        selectedItemColor: Colors.blue, // 设置选中状态下的文字颜色
        unselectedItemColor: Colors.grey, // 设置未选中状态下的文字颜色
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            label: '微信',
            icon: _currentIndex == 0
                ? Image.asset(
              'images/weixin_pressed.png',
              width: 32.0,
              height: 28.0,
            )
                : Image.asset(
              'images/weixin_normal.png',
              width: 32.0,
              height: 28.0,
            ),
          ),
          BottomNavigationBarItem(
            label: '通讯录',
            icon: _currentIndex == 1
                ? Image.asset(
              'images/contact_list_pressed.png',
              width: 32.0,
              height: 28.0,
            )
                : Image.asset(
              'images/contact_list_normal.png',
              width: 32.0,
              height: 28.0,
            ),
          ),
          BottomNavigationBarItem(
            label: '发现',
            icon: _currentIndex == 2
                ? Image.asset(
              'images/find_pressed.png',
              width: 32.0,
              height: 28.0,
            )
                : Image.asset(
              'images/find_normal.png',
              width: 32.0,
              height: 28.0,
            ),
          ),
          BottomNavigationBarItem(
            label: '我',
            icon: _currentIndex == 3
                ? Image.asset(
              'images/profile_pressed.png',
              width: 32.0,
              height: 28.0,
            )
                : Image.asset(
              'images/profile_normal.png',
              width: 32.0,
              height: 28.0,
            ),
          ),
        ],
      ),
      body: currentPage(),
    );
  }
}